package com.cms.recursoshumanos.popup;

import com.cms.recursoshumanos.cache.CarritoCompraCache;
import com.cms.recursoshumanos.component.base.CustomButtonColored;
import com.cms.recursoshumanos.component.base.CustomTextField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.object.Articulo;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NumericChoiceField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class AddCarritoPopUp extends PopupScreen
{
	
	VerticalFieldManager vfm;
	NumericChoiceField ncf;
	HorizontalFieldManager hfm;
	//TwoColumnField tcf;
	CustomTextField cantidad_pago;
	LabelField lblInfoTopPopUp_1,lblInfoTopPopUp_2,lblInfoTopPopUp_3;
	CustomButtonColored boton_ok, boton_cancel;
	

	private Articulo elArticulo;
	
	int start = Constant.CANTIDAD_MINIMA_ARTICULO_ADD;
	int end = Constant.CANTIDAD_MAXIMA_ARTICULO;
	int increment = Constant.STEP_CANTIDAD;
	int setTo =  Constant.NCF_SET_TO;
	
	public static AddCarritoPopUp instance = null;
	
	public static AddCarritoPopUp getInstance(Articulo elArticulo)
	{
		instance = new AddCarritoPopUp(elArticulo);
		return instance;
	}
	public AddCarritoPopUp(Articulo elArticulo)
	{
		super(new VerticalFieldManager(USE_ALL_WIDTH | FIELD_HCENTER), USE_ALL_WIDTH | FIELD_HCENTER);
		this.elArticulo = elArticulo;
		setComponents();
		
	}
	protected boolean keyChar( char key, int status, int time )
    {
        if( key == Characters.ESCAPE ) {
           close();
           return false;
        }
        else
        {

            return true;
        }
    }
	public void setComponents()
	{
		hfm = new HorizontalFieldManager(FIELD_HCENTER);
		lblInfoTopPopUp_1 = new LabelField("Producto:",
											USE_ALL_WIDTH |
											FIELD_HCENTER |
											DrawStyle.HCENTER);
		
		lblInfoTopPopUp_2 = new LabelField("\""+this.elArticulo.getNombreArticulo()+"\"",
				USE_ALL_WIDTH |
				FIELD_HCENTER |
				DrawStyle.HCENTER);
		
		ncf = new NumericChoiceField("Cantidad:", start, elArticulo.getCantidadDisponible(), increment,0)
		{
			
			protected boolean keyChar( char character, int status, int time ) 
		    {
		        if( character == Characters.ENTER ) 
		        {
		            return super.navigationClick(status, time);
		            //return true;
		        }
		        else
		        {
		        	if (character ==  Characters.ESCAPE)
		        	{
		        		close();
		        	}
		        }
		        return super.keyChar( character, status, time );
		    }
		    
		    protected boolean navigationClick( int status, int time ) 
		    {
		        return super.navigationClick(status, time);
		        //return true;    
		    }
		    
		    protected boolean trackwheelClick( int status, int time )
		    {        
		        return super.navigationClick(status, time);    
		        //return true;
		    }
			
		};

		boton_ok = new CustomButtonColored("Agregar al Carrito"
				+ "",ButtonField.CONSUME_CLICK | FIELD_HCENTER, Constant.COLOR_BUTTON_FOCUS); 
		/*boton_cancel = new CustomButtonColored("Regresar"
				+ "",ButtonField.CONSUME_CLICK | FIELD_HCENTER, COLOR_FOCUS); 
        */
        boton_ok.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			ShowDialogAlert alerta;
	   			try
	   			{
		   			CarritoCompraCache.getInstance().addArticuloToCarrito(elArticulo,
		   					ncf.getSelectedValue());
		   			//elArticulo.setCantidadSeleccionado(0);
		   			
		   			close();
		   			alerta = new ShowDialogAlert("Se agrego el Producto: "+elArticulo.getNombreArticulo());
	   			}
	   			catch (Exception ee)
	   			{
	   				alerta = new ShowDialogAlert("Error, no se pudo agregar");
	   			}
	   			alerta.run();
	       	} 
		}); 
       /* boton_cancel.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			instance.close();
	       	} 
		});*/ 
     
        
		//tcf = new TwoColumnField(boton_ok,boton_cancel,USE_ALL_WIDTH);
		
		add(lblInfoTopPopUp_1);
		add(lblInfoTopPopUp_2);
		hfm.add(ncf);
		add(hfm);
		add(boton_ok);
		//hfm.add(boton_ok);
		//hfm.add(boton_cancel);
		//add(hfm);
		//add(tcf);
		
		
		//super(vfm , U);
	}
	// TODO Auto-generated constructor stub}

}
